package com.fl.iocaop.java8.Teststream;
//容器类

import ch.qos.logback.core.subst.Token;
import com.fl.iocaop.domain.Dept;
import com.fl.iocaop.java8.apple;
import org.junit.Test;

import java.util.List;
import java.util.Optional;

/**
 * optional容器类的常用方法:
 * optional.of(T t) :创建一个Optional实例optional.empty() :创建一个空的Optional实例
 * 0ptional.ofNullable(T t):若t不为null,创建Optional 实例,否则创建空实例isPresent() :判断是否包含值
 * orElse(T t) :如果调用对象包含值，返回该值,否则返回t
 * orElseGet(Supplier s) :如果调用对象包含值，返回该值，否则返回s 获取的值
 * map(Function f):如果有值对其处理，并返回处理后的Optional，否则返回Optional.empty()flatMap(Function mapper):与map类似，要求返回值必须是Optional
 */
public class TestOptional {
    private List<apple> apples = TestStreamApI2.methodTest();
    @Test
    public void test(){
        //optional.of(T t) :创建一个Optional实例optional.empty() :创建一个空的Optional实例
        Optional<List<apple>> apples = Optional.of(this.apples);
//        System.out.println("apples = " + apples);
        //orElse根据接收的传参是否为空返回对象，若为空就反正事先定义好的对象，若不为空则返回接收的对象
        Optional<Dept> dept = Optional.ofNullable(new Dept("上海大众", "8208820"));
        Dept data = dept.orElse(new Dept("北京现代", "55555"));
        System.out.println("data = " + data);

    }
    @Test
    public void test2(){
        Optional<Dept> dept = Optional.ofNullable(new Dept("上海大众", "8208820"));
        Optional<String> deptname = dept.map((e) -> e.getDeptName());
        System.out.println("deptname = " + deptname);
    }
    @Test
    public void  test3(){
        Optional<Dept> op = Optional.ofNullable(null);
        System.out.println("TestOptional1(op) = " + TestOptional1(op));
    }
    public String TestOptional1(Optional<Dept> op){
        return op.orElse(new Dept("日立","250"))
                .getDeptName();

    }
}
